package com.client.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.entity.Message;
import com.entity.Result;
import com.util.Command;

public class ClientService {
	private Socket socket;
	private ObjectOutputStream os=null;
	private ObjectInputStream is=null;
	
	public ClientService(){
		try {
			 socket=new Socket("127.0.0.1",2468);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
public Result service(Message message) throws IOException, ClassNotFoundException{
	    os=new ObjectOutputStream(socket.getOutputStream());
	    is=new ObjectInputStream(socket.getInputStream());
		os.writeObject(message);
		os.flush();
		if(message.getOpr().equals(Command.EXIT)){
			os.close();
			is.close();
		}
		Result result=(Result)is.readObject();
		
		return result;
	
}
}
